package com.example.justbuy.action.buyer;


import com.example.justbuy.pojo.Evaluate;
import com.example.justbuy.pojo.Order;
import com.example.justbuy.pojo.User;
import com.example.justbuy.service.EvaluateMapperService;
import com.example.justbuy.service.ImageMapperService;
import com.example.justbuy.service.OrderMapperService;
import com.example.justbuy.util.ConvertDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
public class EvaluateAction {
    @Autowired
    private EvaluateMapperService evaluateMapperService;
    @Autowired
    private OrderMapperService orderMapperService;
    @Autowired
    private ImageMapperService imageMapperService;

    @RequestMapping("/evaluate.htm")
    public String insertEvaluate(Integer id, Integer goodsId, Integer userID,MultipartFile[] items_pic,
                                 String anonymityType, String physicalEvaluate, String goodsEvaluate, String merchantAttitude, String evaluateDse,
                                 Model md, HttpServletRequest request, HttpServletResponse response)throws Exception{
        User user= (User)request.getSession().getAttribute("user");
        Order order=orderMapperService.getById(id);
        HashMap map=new HashMap();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        if(physicalEvaluate==null||physicalEvaluate.equals("")){
            physicalEvaluate="5";
        }
        if(merchantAttitude==null||merchantAttitude.equals("")){
            merchantAttitude="5";
        }
        if(goodsEvaluate==null||goodsEvaluate.equals("")){
            goodsEvaluate="5";
        }

        map.put("addTime", ConvertDateUtil.castDate(date));
        map.put("orderId",order.getOrderId());
        map.put("goodsId",order.getGoodsId());
        map.put("userId",user.getId());
        map.put("anonymityType",anonymityType);
        map.put("physicalEvaluate",physicalEvaluate);
        map.put("goodsEvaluate",goodsEvaluate);
        map.put("merchantAttitude",merchantAttitude);
        map.put("evaluateDse",evaluateDse);
        int flag=evaluateMapperService.inserEvalByOrderId(map);
        if(flag>0){
            Evaluate evaluate=evaluateMapperService.getByMap(map);
            String pic_path = request.getSession().getServletContext().getRealPath("/");
            for (int i=0;i<items_pic.length;i++){
                String originalFilename = items_pic[i].getOriginalFilename();
                if(items_pic!=null && originalFilename!=null && originalFilename.length()>0){
                    //存储图片的物理路径
                    //新的图片名称
                    String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                    md.addAttribute("pic_path",newFileName);
                    //新图片
                    //File newFile = new File(pic_path + newFileName);
                    File newFile = new File("/tmp/" + newFileName);
                    items_pic[i].transferTo(newFile);
                    HashMap map01=new HashMap();
                    SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    String date1 = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
                    map01.put("addTime", ConvertDateUtil.castDate(date1));
                    map01.put("path",newFileName);
                    map01.put("evaluateId",evaluate.getId());
                    imageMapperService.saveEval(map01);
                }
            }
            HashMap map04=new HashMap();
            map.put("id",id);
            map.put("orderState","已完成");
            orderMapperService.updateStateById01(map);
            System.out.println("评价成功！！");
            return "redirect:show_order.htm";
        }else{
            System.out.println("对不起，开小差了，请重新评价!");
            return "redirect:evaleateF.htm";
        }

    }
}
